﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WHC.Hotel.BLL;
using WHC.Hotel.Entity;
using WHC.OrderWater.Commons;

namespace WHC.Hotel.UI
{
    public partial class FrmEditKTVOtherIncome : BaseForm
    {
        public string ID = string.Empty;

        public FrmEditKTVOtherIncome()
        {
            InitializeComponent();

            this.cmbOperateType.Items.Clear();
            this.cmbOperateType.Items.AddRange(DictItemUtil.GetOtherIncomeType());
        }

        private void FrmEditOtherIncome_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ID))
            {
                this.Text = "编辑 " + this.Text;
                KTVOtherIncomeInfo info = BLLFactory<KTVOtherIncome>.Instance.FindByID(ID);
                if (info != null)
                {
                    txtProject.Text = info.Project;
                    cmbOperateType.Text = info.OperateType;
                    txtQuantity.Text = info.Quantity.ToString();
                    txtUnit.Text = info.Unit;
                    txtPrice.Text = info.Price.ToString("f2");
                    txtAmount.Text = info.Amount.ToString("f2");
                    txtNote.Text = info.Note;

                    txtOperateDate.Text = info.OperateDate.ToString();
                    txtOperator.Text = info.Operator;
                    lblLastUpdated.Text = info.LastUpdated.ToString();
                }
                //this.btnOK.Enabled = (Portal.gc.HasFunction("Finance/Income") || Portal.gc.HasFunction("Finance/Pay"));            
            }
            else
            {
                this.Text = "新建 " + this.Text;

                this.lblLastUpdated.Text = DateTime.Now.ToString();
                txtOperateDate.Text = DateTime.Now.ToString();
                txtOperator.Text = Portal.gc.LoginInfo.FullName;

                //this.btnOK.Enabled = (Portal.gc.HasFunction("Finance/Income") || Portal.gc.HasFunction("Finance/Pay"));  
            }
        }

        private void SetInfo(KTVOtherIncomeInfo info)
        {
            info.Project = this.txtProject.Text;
            info.OperateType = this.cmbOperateType.Text;
            info.Unit = this.txtUnit.Text;
            info.Quantity = Convert.ToInt32(this.txtQuantity.Text);
            info.Price = Convert.ToDecimal(this.txtPrice.Text);
            info.Amount = Convert.ToDecimal(txtAmount.Text);
            info.Operator = this.txtOperator.Text;
            info.OperateDate = Convert.ToDateTime(this.txtOperateDate.Text);
            info.Note = txtNote.Text;
            info.LastUpdated = DateTime.Now;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            #region 检查输入
            if (this.txtProject.Text == "")
            {
                MessageUtil.ShowTips("请输入项目名称");
                this.txtProject.Focus();
                return;
            }
            else if (this.cmbOperateType.Text == "")
            {
                MessageUtil.ShowTips("请选择款项类型");
                this.cmbOperateType.Focus();
                return;
            }
            else if (this.txtPrice.Text == "")
            {
                MessageUtil.ShowTips("单价不能为空");
                txtPrice.Focus();
                return;
            }
            else if (this.txtAmount.Text == "")
            {
                MessageUtil.ShowTips("金额不能为空");
                txtAmount.Focus();
                return;
            } 
            #endregion

            if (!string.IsNullOrEmpty(ID))
            {
                KTVOtherIncomeInfo info = BLLFactory<KTVOtherIncome>.Instance.FindByID(ID);
                if (info != null)
                {
                    SetInfo(info);

                    try
                    {
                        bool succeed = BLLFactory<KTVOtherIncome>.Instance.Update(info, info.ID.ToString());
                        if (succeed)
                        {
                            MessageUtil.ShowTips("保存成功");
                            this.DialogResult = DialogResult.OK;
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error(ex);
                        MessageUtil.ShowError(ex.Message);
                    }
                }
            }
            else
            {
                KTVOtherIncomeInfo info = new KTVOtherIncomeInfo();
                SetInfo(info);

                try
                {
                    bool succeed = BLLFactory<KTVOtherIncome>.Instance.Insert(info);
                    if (succeed)
                    {
                        MessageUtil.ShowTips("保存成功");
                        this.DialogResult = DialogResult.OK;
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                    MessageUtil.ShowError(ex.Message);
                }
            }
        }

        private void txtQuantity_TextChanged(object sender, EventArgs e)
        {
            CaculateAmount();
        }

        private void txtPrice_TextChanged(object sender, EventArgs e)
        {
            CaculateAmount();
        }

        private void CaculateAmount()
        {
            if (!string.IsNullOrEmpty(txtQuantity.Text) && ValidateUtil.IsNumeric(txtQuantity.Text)
                && !string.IsNullOrEmpty(txtPrice.Text) && ValidateUtil.IsNumeric(txtPrice.Text))
            {
                decimal Quantity = Convert.ToDecimal(this.txtQuantity.Text);
                decimal Price = Convert.ToDecimal(this.txtPrice.Text);

                this.txtAmount.Text = (Quantity * Price).ToString("f2");
            }
            else
            {
                this.txtAmount.Text = "0";
            }
        }
    }
}
